package com.sx.gongzhong.adapter;


import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sx.gongzhong.R;
import com.sx.gongzhong.model.HelpBean;
import com.sx.gongzhong.net.ServiceFactory;

import java.util.List;

/**
 * 互帮互助adapter
 * Created by XZY on 2017/12/28
 */

public class HelpListAdapter extends BaseQuickAdapter<HelpBean,BaseViewHolder> {

    private Context context;

    public HelpListAdapter(Context context, @LayoutRes int layoutResId, @Nullable List<HelpBean> data) {
        super(layoutResId, data);
        this.context=context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final HelpBean item) {
        helper.setText(R.id.tv_Title,item.getTitle());
        helper.setText(R.id.tv_Content,item.getContent());
        helper.setText(R.id.tv_Time,item.getReleaseTime());
        helper.setText(R.id.tv_Look,item.getReadTimes());

        TextView tv_statu = helper.getView(R.id.tv_handleStatus);
        if (item.getHandleStatus().equals("to_be_solved")){
            tv_statu.setText("待解决");
            tv_statu.setBackgroundResource(R.drawable.shape_red_corner_50);
        }else if (item.getHandleStatus().equals("shut_down")){
            tv_statu.setText("已关闭");
            tv_statu.setBackgroundResource(R.drawable.shape_orige_corner_50);
        }else if (item.getHandleStatus().equals("solved")){
            tv_statu.setText("已解决");
            tv_statu.setBackgroundResource(R.drawable.shape_green_corner_50);
        }

        if (item.getPublisher() != null){
            helper.setText(R.id.tv_Name,item.getPublisher().getName());
            ImageView iv_headerUrl = helper.getView(R.id.im_Head);
            RequestOptions options = new RequestOptions().centerCrop()
                    .placeholder(R.mipmap.img_head_default)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .circleCrop()
                    .error(R.mipmap.img_head_default);
            Glide.with(context)
                    .load(ServiceFactory.getUrl(item.getPublisher().getHeaderUrl()))
                    .apply(options)
                    .into(iv_headerUrl);
        }
        TextView tv_Num=helper.getView(R.id.tv_Num);
        tv_Num.setVisibility(View.GONE);

        ImageView image1=helper.getView(R.id.imageView1);
        LinearLayout ll2=helper.getView(R.id.ll2);
        LinearLayout ll3=helper.getView(R.id.ll3);
        image1.setVisibility(View.GONE);
        ll2.setVisibility(View.GONE);
        ll3.setVisibility(View.GONE);
        if (item.getFiles() != null && item.getFiles().size() > 0){
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.img_fail)
                    .error(R.mipmap.img_fail);
            if (item.getFiles().size()==1){
                image1.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(0).getPath()))
                        .apply(options)
                        .into(image1);
            }
            if (item.getFiles().size()==2){
                ll2.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(0).getPath()) )
                        .apply(options)
                        .into((ImageView) helper.getView(R.id.imageView21));
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(1).getPath()))
                        .apply(options)
                        .into((ImageView) helper.getView(R.id.imageView22));
            }
            if (item.getFiles().size()>=3){
                if (item.getFiles().size() > 3) {
                    tv_Num.setVisibility(View.VISIBLE);
                    tv_Num.setText("+ " + (item.getFiles().size() - 3));
                }
                ll3.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(0).getPath()))
                        .apply(options)
                        .into((ImageView)helper.getView(R.id.imageView31));
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(1).getPath())  )
                        .apply(options)
                        .into((ImageView)helper.getView(R.id.imageView32));
                Glide.with(context)
                        .load(ServiceFactory.getUrl(item.getFiles().get(2).getPath()))
                        .apply(options)
                        .into((ImageView)helper.getView(R.id.imageView33));
            }
        }
    }

}
